package com.bangcommunity.bbframe.media;

import com.bangcommunity.bbframe.fdfs.FdfsClient;
import com.bangcommunity.bbframe.fdfs.FdfsClientUtil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author tanghc
 * @date 18/6/26
 */
public class JavaCvTest1 {
    static FdfsClient fdfsClient = FdfsClientUtil.getFdfsClient();

    public static void main(String[] args) throws Exception {

        // randomGrabberFFmpegImage("e:/ffmpeg/aa.mp4", "./target", "screenshot", 5);

//        randomGrabberFFmpegImage("/opt/data/v1.mp4", "/opt/data", "screenshot", 1);
        randomGrabberFFmpegImage("/opt/data/v1.mp4", "/opt/data", "screenshot");

    }

    public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName ) throws Exception {
        // FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        String fullId = "group1/M00/00/00/wKgArVsyO66Afve1AFlBT_7_PUA7889376";
         FFmpegFrameGrabber ff = new FFmpegFrameGrabber(fdfsClient.getFdfsSourceInputStream(fullId,0,128*1024));
//        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(new FileInputStream(new File("/opt/data/v1.mp4")));
        long t = System.currentTimeMillis();
        ff.start();
        System.out.println("t1:" + (System.currentTimeMillis() - t));
        Frame  f = ff.grabImage();
        t = System.currentTimeMillis();
        doExecuteFrame(f, targerFilePath, targetFileName, 1);
        System.out.println("t:" + (System.currentTimeMillis() - t));
        System.out.println("time:" + ff.getLengthInTime() + ";w:" + ff.getImageWidth() + ";h:" + ff.getImageHeight());

        ff.stop();
    }
    public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName,
            int randomSize) throws Exception {
        // FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        String fullId = "group1/M00/00/00/wKgArVsyO66Afve1AFlBT_7_PUA7889376";
         FFmpegFrameGrabber ff = new FFmpegFrameGrabber(fdfsClient.getFdfsSourceInputStream(fullId,0,512*1024));
//        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(new FileInputStream(new File("/opt/data/v1.mp4")));
        long t = System.currentTimeMillis();
        ff.start();
        System.out.println("t1:" + (System.currentTimeMillis() - t));
        int ffLength = ff.getLengthInFrames();
        List<Integer> randomGrab = random(ffLength, randomSize);
        int maxRandomGrab = randomGrab.get(randomGrab.size() - 1);
        Frame f;
        int i = 0;
        while (i < ffLength) {
            f = ff.grabImage();
            if (randomGrab.contains(i)) {
                t = System.currentTimeMillis();
                doExecuteFrame(f, targerFilePath, targetFileName, i);
                System.out.println("t:" + (System.currentTimeMillis() - t));
            }
            if (i >= maxRandomGrab) {
                break;
            }
            i++;
        }
        System.out.println("time:" + ff.getLengthInTime() + ";w:" + ff.getImageWidth() + ";h:" + ff.getImageHeight());

        ff.stop();
    }

    public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName, int index) {
        if (null == f || null == f.image) {
            return;
        }

        Java2DFrameConverter converter = new Java2DFrameConverter();

        String imageMat = "png";
        String FileName = targerFilePath + File.separator + targetFileName + "_" + index + "." + imageMat;
        BufferedImage bi = converter.getBufferedImage(f);
        File output = new File(FileName);
        try {
            ImageIO.write(bi, imageMat, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Integer> random(int baseNum, int length) {
        List<Integer> list = new ArrayList<>(length);
        while (list.size() < length) {
            Integer next = (int) (Math.random() * baseNum);
            if (list.contains(next)) {
                continue;
            }
            list.add(next);
        }
        Collections.sort(list);
        return list;
    }
}
